package com.power.web.action.member;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

import org.jgroups.protocols.PRINTOBJS;
import org.springframework.beans.factory.annotation.Autowired;

import com.power.Constants;
import com.power.model.member.TmFamilyinfo;
import com.power.model.user.TsSysuser;
import com.power.services.member.TmFamilyinfoService;
import com.power.util.CharacterUtil;
import com.power.util.DateJsonValueProcessor;
import com.power.util.DateTimeUtil;
import com.power.util.RequestUtil;
import com.power.web.action.base.AbstractBaseAction;

public class TmFamilyinfoAction extends AbstractBaseAction {

	private static final long serialVersionUID = 1L;
	@Autowired
	public TmFamilyinfoService service;
	private String jsonString;

	private TmFamilyinfo family;

	@SuppressWarnings("unchecked")
	public String query4JSONString() {
		try {
			int start = this.start;
			int limit = this.limit;
			if (limit == 0)
				limit = 20;
			String type = CharacterUtil.encode(RequestUtil.getParam(request,
					"type", ""));
			Map<String, Object> map = this.service.findAll(type, start, limit);
			List<TmFamilyinfo> list = (List<TmFamilyinfo>) map.get("data");
			int count = new Integer(map.get("count").toString());
			StringBuffer _JSONStr = null;
			String callback = this.callback;
			_JSONStr = new StringBuffer();
			_JSONStr.append(callback).append("({\"total\":").append(count)
					.append(",\"results\":");
			JsonConfig cfg = new JsonConfig();
			cfg.registerJsonValueProcessor("inputDate",
					new DateJsonValueProcessor("yyyy-MM-dd kk:mm:ss"));
			cfg.registerJsonValueProcessor(java.util.Date.class,
					new DateJsonValueProcessor("yyyy-MM-dd"));
			cfg.registerJsonValueProcessor(java.sql.Date.class,
					new DateJsonValueProcessor("yyyy-MM-dd"));
			JSONArray jsonObject = JSONArray.fromObject(list, cfg);
			_JSONStr.append(jsonObject.toString());
			_JSONStr.append("})");
			jsonString = _JSONStr.toString();
		} catch (Exception e) {
			log.error("加载导医列表出错", e);
			this.jsonString = "[]";
		}
		return Constants.JSONSTRING_RESULT_NAME;
	}

	public String toInfo() {
		TsSysuser user = (TsSysuser) this.session.get(Constants.USERINFO_KEY);
		String type = RequestUtil.getParam(request, "type", "1");
		try {
		Integer memId = RequestUtil.getParam(request, "memId", user.getId());
			family = service.findByMemId(memId);
			if ("1".equals(type)) {
				if (null == family)
					type = "2";
			}
		} catch (Exception e) {
			log.error("显示家庭信息出错", e);
		}
		if ("1".equals(type)) {
			return "info";
		} else {
			return "edit";
		}
	}

	public String showDetail() {
		TsSysuser user = (TsSysuser) this.session.get(Constants.USERINFO_KEY);
		try {
		Integer memId = RequestUtil.getParam(request, "memId", user.getId());
		
			family = service.findByMemId(memId);
		} catch (Exception e) {
			log.error("显示家庭信息出错", e);
		}
		return "showDetail";
	}

	public String save() {
		TsSysuser user = (TsSysuser) this.session.get(Constants.USERINFO_KEY);
		try {
			family.setUserId(user.getId());
			family.setAuthorName(user.getUsername());
			if (null == family.getId() || 0 > family.getId()) {
				family.setCreateTime(DateTimeUtil.getCurrentDateTime());
				family.setIsDual("2");
			}
			service.saveOrUpdate(family);
		} catch (Exception e) {
			log.error("家庭信息保存失败", e);
		}
		return "saveSuccess";
	}
	
	public String deleteInfo() {
		this.response.reset();
		this.response.setCharacterEncoding("UTF-8");
		String ids = RequestUtil.getParam(request, "ids", "");
		try {
			service.deleteInfo(ids);
			this.response.getWriter().print("{success:true,msg:'删除成功!'}");
		} catch (Exception e) {
			try {
				if ("1".equals(e.getMessage())) {
					this.response.getWriter().print(
							"{success:false,msg:'删除失败!'}");
				}
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		return null;
	}
	
	public String dualInfo(){
		Integer fid = RequestUtil.getParam(request, "fid", 0);
		PrintWriter out = null;
		response.reset();
		response.setCharacterEncoding("utf-8");
		try{
			out = response.getWriter();
			family = service.read(fid);
			family.setIsDual("1");
			service.update(family);
			out.print("<script>parent.closeTabPage();</script>");
		}catch (Exception e) {
			log.error("", e);
		}
		return null;
	}

	public String getJsonString() {
		return jsonString;
	}

	public TmFamilyinfo getFamily() {
		return family;
	}

	public void setFamily(TmFamilyinfo family) {
		this.family = family;
	}

	public void setJsonString(String jsonString) {
		this.jsonString = jsonString;
	}

}
